//
//  TrainOrderCenterController.swift
//  AirTravel
//
//  Created by apple on 2021/8/24.
//

import UIKit
import Parchment

class TrainOrderCenterController: UIViewController {

    @IBOutlet weak var backView: UIView!
    let titleArray :[String] = ["待支付","已支付","支付失败","待出票","已出票","已取消","已完成","出票失败"]
    
    lazy var pagingView: PagingViewController = {
        let pagingViewController = PagingViewController()
        pagingViewController.dataSource = self
        pagingViewController.delegate = self
        pagingViewController.menuItemSize = .selfSizing(estimatedWidth: SCREEN_WIDTH / 5, height: 40)
        pagingViewController.font = UIFont.systemFont(ofSize: 15)
        pagingViewController.selectedFont = UIFont.systemFont(ofSize: 15)
        pagingViewController.selectedTextColor = Blue_COLOR
        pagingViewController.indicatorColor = Blue_COLOR
        pagingViewController.menuItemLabelSpacing = 8
        pagingViewController.indicatorOptions = .visible(
          height: 2,
          zIndex: Int.max,
          spacing: UIEdgeInsets.init(top: 0, left: 12, bottom: 0, right: 12),
          insets: UIEdgeInsets.zero
        )
        return pagingViewController
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.black
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.black]
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.white]
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "火车票订单"
        setUppagingView()
    }


    func setUppagingView() {
        addChild(self.pagingView)
        self.backView.addSubview(self.pagingView.view)
        self.backView.constrainToEdges(self.pagingView.view)
        self.pagingView.didMove(toParent: self)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            NotificationCenter.default.post(name: NSNotification.Name("SecondsKillNotification"), object: nil, userInfo: ["intro":"0"])
        }
    }

}

extension TrainOrderCenterController: PagingViewControllerDataSource,PagingViewControllerDelegate {
  func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
    let str = self.titleArray[index]
    return PagingIndexItem(index: index, title: str)
  }
  
  func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
     return TrainOrderListController()
  }
  
  func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
    return titleArray.count
  }
    func pagingViewController(
      _ pagingViewController: PagingViewController,
      didScrollToItem pagingItem: PagingItem,
      startingViewController: UIViewController?,
      destinationViewController: UIViewController,
        transitionSuccessful: Bool){
        if transitionSuccessful {
            let item = pagingItem as! PagingIndexItem
//            let str = self.titleArray[item.index]
            NotificationCenter.default.post(name: NSNotification.Name("SecondsKillNotification"), object: nil, userInfo: ["intro":"\(item.index)"])
        }
    }
}
